package com.luo.storage.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.luo.storage.utils.JwtTool;
import com.luo.storage.utils.UserContext;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class DatabaseMetaObjectHandler implements MetaObjectHandler {

    @Autowired
    private UserContext userContext;

    @Override
    public void insertFill(MetaObject metaObject){
        this.strictInsertFill(metaObject,"createTime", LocalDateTime.class,LocalDateTime.now());
        this.strictInsertFill(metaObject,"createBy",String.class,getCurrentUserId());
        this.strictInsertFill(metaObject,"updateTime", LocalDateTime.class,LocalDateTime.now());
        this.strictInsertFill(metaObject,"updateBy",String.class,getCurrentUserId());
    }

    @Override
    public void updateFill(MetaObject metaObject){
        this.strictUpdateFill(metaObject,"updateTime",LocalDateTime.class,LocalDateTime.now());
        this.strictUpdateFill(metaObject,"updateBy",String.class,getCurrentUserId());
    }

    private String getCurrentUserId(){
        Long userId = userContext.getUserId();
        return userId!=null?userId.toString():"未知操作者";
    }

}
